package com.qingsong.mapper;

import com.github.pagehelper.PageInfo;
import com.qingsong.dto.PageDTO;
import com.qingsong.entity.EmpRole;
import com.qingsong.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RoleMapper {

    @Insert("INSERT INTO ums_role (`title`,`info`) " +
            "VALUES" +
            " (#{title},#{info})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(Role role);

    @Select("select us.id, us.title, us.info from ums_role us "+
            "where us.id = #{param1} and us.deleted = 0")
    Role selectById(Long id);

    @Update("<script>" +
            "update ums_role " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if>" +
            "<if test='info != null'> info = #{info}, </if>" +
            "<if test='deleted != null'> deleted = #{deleted}, </if>" +
            "<if test='created != null'> created = #{created}, </if>" +
            "<if test='updated != null'> updated = #{updated}, </if>" +
            "</set> " +
            "where id = #{id}" +
            "</script>")
    int updateById(Role role);

    @Update("<script>" +
            "update ums_role set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Update("update ums_role set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Select("select us.id, us.title, us.info, us.deleted " +
            "from ums_role us "+
            "where us.deleted = 0")
    List<Role> list();

    @Select("select us.id, us.title, us.info " +
            "from ums_role us "+
            "where us.deleted = 0")
    PageInfo<Role> page(PageDTO pageDTO);

    @Select("select us.id, us.title, us.info " +
            "from ums_role us " +
            "where us.title = #{title} ")
    Role selectByAllTitle(Role role);

    @Delete("delete from ums_role where id = #{param1}")
    int deleteById(Long id);

    @Select("select us.id, us.title, us.info, " +
            "us.deleted, us.created, us.updated from ums_role us "+
            "where us.deleted = 0 and us.title like concat('%', #{param1}, '%')")
    List<Role> listLikeTitle(String title);

    @Select("select ur.id, ur.title, ur.info, ur.deleted, ur.created, ur.updated " +
            "from ums_emp_role uer" +
            " join ums_role ur on uer.fk_role_id = ur.id" +
            " join ums_emp ue on uer.fk_emp_id = ue.id " +
            "where uer.fk_emp_id = #{param1} and ur.deleted = 0 and ue.deleted = 0" +
            "  and uer.deleted = 0")
    List<Role> selectByEmpId(Long empId);

    @Update("update ums_emp_role set deleted = 1, updated = now() where fk_emp_id = #{empId}")
    int loginDeleteByEmpId(Long empId);


    @Insert("INSERT INTO ums_emp_role (`fk_emp_id`,`fk_role_id`,`deleted`,`created`,`updated`) " +
            "VALUES" +
            " (#{fkEmpId},#{fkRoleId},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insertEmpRole(EmpRole empRole);

    @Update("update ums_emp_role set deleted = 1, updated = now() where fk_emp_id = #{fkEmpId}")
    void existsEmpId(Long fkEmpId);

    @Update("<script>" +
            "update ums_emp_role set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='us.fk_emp_id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    void existsEmpIds(List<Long> fkEmpIds);

    @Update("update ums_emp_role set deleted = 1, updated = now() where fk_role_id = #{fkRoleId}")
    void existsRoleId(Long fkRoleId);

    @Update("<script>" +
            "update ums_emp_role set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='us.fk_role_id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    void existsRoleIds(List<Long> fkRoleIds);
}
